import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, getConnection } from 'typeorm';
import { UserRole as Entity } from '@/entity/user/user_role.entity';

@Injectable()
export class RoleDao {
 
    public connection = getConnection();
    constructor(@InjectRepository(Entity)
    private readonly repository: Repository<Entity>,
    ) { }


    /**
     * 查询所有
     */
    findAll(param:any): Promise<Entity[]> {
        let sql="select R.*, array_to_string( array( select permission_id from user_permission_relation gi where role_id=R.id),',') as permission_ids from user_role as R "
        +"order by R.id desc";
        return this.connection.query(sql);
    }

    /**
     * 添加
     */
    add(params:any):Promise<Entity>{
        let entity = new Entity();
        Object.assign(entity,params);
        return this.repository.save(entity);
    }

    /**
     * 修改
     * @param params 
     */
    async update(params:any):Promise<Entity>{
        let entity = await this.repository.findOne(params.id);
        Object.assign(entity,params);
        return this.repository.save(entity);
    }

    /**
     * 删除
     * @param params 
     */
    async delete(params:any){
        return this.repository.delete(params);
    }

}
